$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/authorapply/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 30, key: true },
			{ label: '用户名', name: 'username', index: 'username', width: 70 },
            { label: '昵称', name: 'nickname', index: 'nickname', width: 50 },
			{ label: '创建时间', name: 'createdate', index: 'createdate', width: 80 },

            { label: '作者申请类型', name: 'type', index: 'type', width: 40,
				formatter: function(value, options, row){
                return value === 0 ?
                    '<span class="label label-danger">普通作者申请</span>' :
                    '<span class="label label-success">签约作者申请</span>';
            } },
			{ label: '申请状态', name: 'status', index: 'status', width: 30,
                formatter: function(value, options, row) {
                    if (value === 0) {
                        return '<span class="label label-primary">已申请</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-success">已通过</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-danger">已拒绝</span>';
                    }
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		authorApply: {
		    text:'',
            id:''
        },
        q:{
            username : null
        },
        sels: [
            {value:"0",week:"已申请"},
            {value:"1",week:"已通过"},
            {value:"2",week:"已拒绝"}
        ],
        showDetail:false,
        userDetail:[{}],
        nowUserId:'',
        littleBrief:{},
        showBreif:false,
	},
	methods: {
		query: function () {
			vm.reload();
		},
        diyContent:function (data) {
		    var str=''
            // 处理内容
            try {
                console.log('in')
                str = data.replace(/(\r\n)|(\n)/g,'<br/>');
            } catch (e) {
                console.log('wrong')
            }
            console.log(str)
            return str;
        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id,type=1)
		},
        cateName: function () {
            vm.showList = true;
            vm.showDom = 'list'
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'status': this.authorApply.status
                },
                page: page
            }).trigger("reloadGrid");
        },
        pass:function(event){
            var id = vm.authorApply.id;
            var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}

            vm.authorApply.text=Base64.encode(vm.authorApply.text)
            $.ajax({
                type: "POST",
                url: baseURL + "sys/authorapply/pass",
                contentType: "application/json",
                data: JSON.stringify(vm.authorApply),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        closeDetail:function(index){
            event.stopPropagation()
              if(index){
                    console.log('notClose')
              }else{
                  this.showDetail=false;
                  this.userDetail=[{}]
              }
        },
        closeBreif:function(index){
            event.stopPropagation()
            if(index){
                console.log('notClose')
            }else{
                this.showBreif=false;
                this.littleBrief=[{}]
            }
        },
        info:function(id){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.getInfo(id);
        },
        reason:function(id){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.getRefuse(id);
        },
        refuse:function(event){
            var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}

            vm.authorApply.text=Base64.encode(vm.authorApply.text);
            console.log(vm.authorApply)
            var id = vm.authorApply.id;
            $.ajax({
                type: "POST",
                url: baseURL + "sys/authorapply/refuse",
                contentType: "application/json",
                data: JSON.stringify(vm.authorApply),
                success: function(r){
                    if(r.code === 0){
                        console.log(888888)
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/authorapply/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        apply: function (event) {
            confirm('确定申请成为签约作者？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/authorapply/apply",
                    contentType: "application/json",
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getRefuse: function(id){
            $.get(baseURL + "sys/authorapply/info/"+id, function(r){
                vm.getRefuseReason(r.authorApply.id)
            });
        },
		getInfo: function(id,type=0){
			$.get(baseURL + "sys/authorapply/info/"+id, function(r){
                vm.authorApply = r.authorApply;
                console.log(type)
                if(!type){
                    vm.getBooks(r.authorApply.id)
                }

            });
		},
        getBooks: function(id){
            $.get(baseURL + "sys/authorapply/signedInfo/"+id, function(r){
                console.log(r)
                if(r.book){
                    vm.userDetail=r.book;
                    vm.showDetail=true;
                }
                if(r.msg) {
                    alert(r.msg);
                }
                if(r.data){
                    vm.littleBrief=r.data;
                    vm.showBreif=true;
                }
            });
        },
        getRefuseReason: function(id){
            console.log(id)
            $.get(baseURL + "sys/authorapply/refuseReasonInfo/"+id, function(r){
                if(r.code==500){
                    alert(r.msg);
                }else {
                    alert('拒绝原因：'+r.refuseReason);
                }

            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
		}
	}
});